# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\Crypto\Random\random.py
"""A cryptographically strong version of Python's standard "random" module."""
__revision__ = "$Id$"
__all__ = [
    "StrongRandom",
    "getrandbits",
    "randrange",
    "randint",
    "choice",
    "shuffle",
    "sample",
]
from Crypto import Random
import sys

if sys.version_info[0] == 2:
    if sys.version_info[1] == 1:
        from Crypto.Util.py21compat import *


class StrongRandom(object):
    def __init__(self, rng=None, randfunc=None):
        if randfunc is None:
            if rng is None:
                self._randfunc = None
            if randfunc is not None:
                if rng is None:
                    self._randfunc = randfunc
                if randfunc is None:
                    if rng is not None:
                        self._randfunc = rng.read
                    raise ValueError("Cannot specify both 'rng' and 'randfunc'")

    def getrandbits(self, k):
        """Return a python long integer with k random bits."""
        if self._randfunc is None:
            self._randfunc = Random.new().read
        mask = (1 << k) - 1
        return mask & bytes_to_long(self._randfunc(ceil_div(k, 8)))

    def randrange(self, *args):
        """randrange([start,] stop[, step]):
        Return a randomly-selected element from range(start, stop, step)."""
        if len(args) == 3:
            start, stop, step = args
        else:
            if len(args) == 2:
                start, stop = args
                step = 1
            else:
                if len(args) == 1:
                    stop, = args
                    start = 0
                    step = 1
                else:
                    raise TypeError(
                        "randrange expected at most 3 arguments, got %d" % (len(args),)
                    )
                if (
                    not isinstance(start, int)
                    or not isinstance(stop, int)
                    or not isinstance(step, int)
                ):
                    raise TypeError("randrange requires integer arguments")
                if step == 0:
                    raise ValueError("randrange step argument must not be zero")
                num_choices = ceil_div(stop - start, step)
                if num_choices < 0:
                    num_choices = 0
                if num_choices < 1:
                    raise ValueError(
                        "empty range for randrange(%r, %r, %r)" % (start, stop, step)
                    )
                r = num_choices
                while r >= num_choices:
                    r = self.getrandbits(size(num_choices))

                return start + step * r

    def randint(self, a, b):
        """Return a random integer N such that a <= N <= b."""
        if not isinstance(a, int) or not isinstance(b, int):
            raise TypeError("randint requires integer arguments")
        N = self.randrange(a, b + 1)
        if not a <= N <= b:
            raise AssertionError
        return N

    def choice(self, seq):
        """Return a random element from a (non-empty) sequence.
        
        If the seqence is empty, raises IndexError.
        """
        if len(seq) == 0:
            raise IndexError("empty sequence")
        return seq[self.randrange(len(seq))]

    def shuffle(self, x):
        """Shuffle the sequence in place."""
        items = list(x)
        for i in range(len(x)):
            x[i] = items.pop(self.randrange(len(items)))

    def sample(self, population, k):
        """Return a k-length list of unique elements chosen from the population sequence."""
        num_choices = len(population)
        if k > num_choices:
            raise ValueError("sample larger than population")
        retval = []
        selected = {}
        for i in range(k):
            r = None
            while r is None or r in selected:
                r = self.randrange(num_choices)

            retval.append(population[r])
            selected[r] = 1

        return retval


_r = StrongRandom()
getrandbits = _r.getrandbits
randrange = _r.randrange
randint = _r.randint
choice = _r.choice
shuffle = _r.shuffle
sample = _r.sample
from Crypto.Util.number import ceil_div, bytes_to_long, long_to_bytes, size
